"use strict";
var express = require('express');
var router = express.Router();
var request = require('request').defaults({
    json: true
});
var config = require('./../../config.json');
var uid = require('uid');
var nameSpace = config.redisNamespace;
var authorUrl = config.authorUrl;
var redis = require('redis');
/** 登陆的login主页 */
router.get('/', function(req, res, next) {
    res.clearCookie('userName');
    res.clearCookie('uid');
    res.render('login.jade');
});

/** 判断是否用户名和密码匹配 */
router.post('/matchCode', function(req, res) {
    var request = require('request');
    var redirect = req.body.redirect ? '/' + decodeURI(req.body.redirect) : '/';
    request({
        url: authorUrl + "/login",
        method: 'post',
        json: {
            userName: req.body.userName,
            password: req.body.password
        }
    }, function(error, response, body) {
        if (!error && response.statusCode === 200) {
            //通过验证
            console.log('log in success!!!');
            res.cookie('userName', body.userName, {
                expires: new Date(Date.now() + 3600 * 1000 * 24)
            });
            res.cookie('uid', body.uid, {
                expires: new Date(Date.now() + 3600 * 1000 * 24)
            });
            res.send(true);
        } else {
            res.send(false);
            console.log('fail to log in');
        }
    });
});


module.exports = router;
